package com.gxwz.document.business.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gxwz.document.business.entity.LoginRecord;
import com.gxwz.document.business.entity.User;
import com.gxwz.document.business.service.IloginRecordService;
import com.gxwz.document.business.service.IuserService;
import com.gxwz.document.core.action.BaseAction;

@SuppressWarnings("serial")
public class LoginAction extends BaseAction {
	private final String INDEX_JSP = "index.jsp";
	private final String LOGIN_RECORD = "/WEB-INF/page/loginRecord/LoginRecord.jsp";
	private final String LOGINERROR_JSP = "loginFail.jsp";
	
	@Resource(name = "userService")
	IuserService userService;
	@Resource(name = "loginRecordService")
	IloginRecordService loginRecordService;
	
	User user;
	List<User> users;
	LoginRecord loginRecord;
	List<LoginRecord> loginRecords;
	HttpSession session = ServletActionContext.getRequest().getSession();
	private int totalRow  = 0;
	
	public String  login(){
		users = userService.findByUserNameAndPassWord(user);		
		if(!users.isEmpty()){
			user = users.get(0);
			session.setAttribute("user", user);
			
			forwardView = INDEX_JSP;
			setForwardView(INDEX_JSP);
			return SUCCESS;
		}
		forwardView = LOGINERROR_JSP;
		/*setForwardView(LOGINERROR_JSP);*/
		return ERROR;
	}
	
	/**
	 * 跳转到登录日志列表页面
	 * @return
	 */
	public String openLoginRecord(){
		
		forwardView = LOGIN_RECORD;
		loginRecords = loginRecordService.findByPage(0, 5);
		return SUCCESS;
	}
	
	/**
	 * 日志列表分页查询
	 */
	public String list(){
		row = (page-1)*DEFUALT_PAGE_SIZE; 
		totalRow = loginRecordService.getRowNum();
		if(page>(loginRecordService.getRowNum()/DEFUALT_PAGE_SIZE+1)){
			page--;
		}
		if(row > loginRecordService.getRowNum() || maxpage == 1){
			loginRecords = loginRecordService.findByPage((loginRecordService.getRowNum()-(loginRecordService.getRowNum()%DEFUALT_PAGE_SIZE)),DEFUALT_PAGE_SIZE);
		}else {
			loginRecords = loginRecordService.findByPage(row,DEFUALT_PAGE_SIZE);
		}
		setForwardView(LOGIN_RECORD);
		return SUCCESS;
	}

	/**
	 * 日志删除
	 */
	public String delete() throws Exception{
		loginRecordService.delete(loginRecord.getId());
		return list();
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public LoginRecord getLoginRecord() {
		return loginRecord;
	}

	public void setLoginRecord(LoginRecord loginRecord) {
		this.loginRecord = loginRecord;
	}

	public List<LoginRecord> getLoginRecords() {
		return loginRecords;
	}

	public void setLoginRecords(List<LoginRecord> loginRecords) {
		this.loginRecords = loginRecords;
	}

	public int getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}
	
	


}
